clear
use cces--combined.dta

encode state,gen(statenum)
gen id=year*100+statenum


* did respondent vote for own party or other party candidate? *
gen partyvote=.
replace partyvote=0 if vote==0 & pid3==2
replace partyvote=0 if vote==1 & pid3==0
replace partyvote=1 if vote==0 & pid3==0
replace partyvote=1 if vote==1 & pid3==2
label define pv 0 "other party" 1 "own party"
label values partyvote pv

* incumbency *
replace incumb=-incumb if pid3==0
replace incumb=. if pid3==1

* distance from own *
gen owndist=.
replace owndist=abs(respideo-demideomean) if pid3==0
replace owndist=abs(respideo-repideomean) if pid3==2
gen owndistsq=owndist^2

* distance from other *
gen othdist=.
replace othdist=abs(respideo-demideomean) if pid3==2
replace othdist=abs(respideo-repideomean) if pid3==0
gen othdistsq=othdist^2

* proximity advantage *
gen proxadv=othdist-owndist

* perceived distance *
gen perowndist=.
replace perowndist=abs(respideo-demideo) if pid3==0
replace perowndist=abs(respideo-repideo) if pid3==2

gen perothdist=.
replace perothdist=abs(respideo-demideo) if pid3==2
replace perothdist=abs(respideo-repideo) if pid3==0



**** ANALYSIS ****
keep if partyvote~=.
keep if incumb~=.
keep if owndist~=.
keep if othdist~=.
keep if pid3~=1


* Table 1 estimates *
logit partyvote incumb proxadv [pw=weight],base cl(id) 
margins ,at(incumb=(0) proxadv=(0))

* Table 2 estimates *
logit partyvote incumb owndist othdist [pw=weight],base cl(id) 
test owndist=-othdist
lincom othdist+owndist

* Figure 1 *
margins ,at(othdist=(0(.5)5) incumb=(0))
marginsplot, 	///
	recast(line) plotopts(lcolor(black)) recastci(rarea) ciopts(fcolor(gs14) lcolor(white)) 	///
	ytitle(Probability of voting for own party candidate) ytitle(, margin(medsmall)) 	///
	ylabel(.5(.1)1, angle(horizontal) format(%3.1f) nogrid) 	///
	xtitle(Distance from other party's candidate) xtitle(, margin(medsmall)) 	///
	xlabel(0(1)5) title(" ") graphregion(fcolor(white) lcolor(black))
margins ,at(owndist=(0(.5)5) incumb=(0))
marginsplot, 	///
	recast(line) plotopts(lcolor(black)) recastci(rarea) ciopts(fcolor(gs14) lcolor(white)) 	///
	ytitle(Probability of voting for own party candidate) ytitle(, margin(medsmall)) 	///
	ylabel(.5(.1)1, angle(horizontal) format(%3.1f) nogrid) 	///
	xtitle(Distance from own party's candidate) xtitle(, margin(medsmall)) 	///
	xlabel(0(1)5) title(" ") graphregion(fcolor(white) lcolor(black))
	
* Figure 5 *
twoway 	///
	(lpolyci perowndist owndist [aweight = weight], bwidth(.75) lcolor(black) clwidth(medthick) clpattern(solid) fcolor(gs12) alcolor(gs12)) 	///
	(lpolyci perothdist othdist [aweight = weight], bwidth(.75) lcolor(black) clwidth(medthick) clpattern(shortdash) fcolor(gs12) alcolor(gs12))	///
	, ytitle(Perceived ideological distance from candidate) 	///
	ytitle(, size(small) margin(medsmall)) ylabel(0(1)6, labsize(small) angle(horizontal) glcolor(gs14) gmin gmax) 	///
	xtitle(Actual ideological distance from candidate) xtitle(, size(small) margin(medsmall)) 	///
	xlabel(0(1)5, labsize(small)) legend(size(small)) xsize(5) ysize(5) graphregion(fcolor(white) lcolor(black))
